<template>
  <div class="create-post-container">
    <el-form ref="postForm" :model="postForm" label-position="right" label-width="100px" class="material-form">
      <el-row :gutter="20">
        <el-col :span="18" :offset="3">
          <el-form-item label="标题" prop="title">
            <el-input v-model="postForm.title" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="18" :offset="3">
          <el-form-item label="MediaID" prop="wzmediaid">
            <el-input v-model="postForm.wzmediaid" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="18" :offset="3">
          <el-form-item label="作者" prop="author">
            <el-input v-model="postForm.author" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="18" :offset="3">
          <el-form-item label="描述" prop="notes">
            <el-input v-model="postForm.notes" :disabled="true" type="textarea" rows="4" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="18" :offset="3">
          <el-form-item label="内容" prop="content">
            <el-card class="box-card" style="background-color: #e4e7ed;">
              <div class="rich-text" v-html="postForm.content" />
            </el-card>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20" class="mt30">
        <el-col :span="18" :offset="3">
          <el-form-item>
            <el-button @click="goBack">返回</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {
  getMaterials
} from '@/api/materials'

const defaultForm = {
  id: undefined,
  title: '',
  wzmediaid: '',
  author: '',
  notes: '',
  content: '',
  state: 0,
  share: 0
}

export default {
  name: 'MaterialDetail',
  props: {
    materialId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      postForm: Object.assign({}, defaultForm),
      loading: false
    }
  },
  created() {
    this.fetchData(this.materialId)
  },
  methods: {
    fetchData(id) {
      getMaterials({ id }).then(response => {
        const { data } = response
        // console.log(data)
        this.postForm.id = data[0].id
        this.postForm.title = data[0].title
        this.postForm.wzmediaid = data[0].wzmediaid
        this.postForm.author = data[0].author
        this.postForm.notes = data[0].notes
        this.postForm.content = data[0].content.replace(/data-src/g, 'src')
      }).catch(err => {
        console.log(err)
      })
    },
    goBack() {
      this.$router.go(-1)
    }
  }
}
</script>

<style lang="scss" scoped>
.create-post-container {
  position: relative;
  .material-form {
    padding: 40px;
  }
}
.rich-text {
  max-width: 100% !important;
  word-break: break-all;
  & /deep/ * {
    max-width: 100% !important;
    word-break: break-all;
  }
}
</style>
